//
//  TF_LivingServicePageView.swift
//  TalkfunSDKDemo
//
//  Created by 莫瑞权 on 2022/2/21.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit


class TF_CustomerService: UIView {

    /**
      身体唯一标识
      默认:0
     */
    
    @objc public var xid = ""

    /**
     未读内容数量
      默认:0
     */
    @objc public var unread:NSInteger = 0

    /**
      聊天记录
      默认列表为空
     */
    
    @objc public var chatList:NSMutableArray =  NSMutableArray()

    /**
      最后收到消息的时间戳
      默认:0
     */
    @objc public var timestamp:NSString  = ""
   
    /**
      最后收到消息
      默认:@""
     */
    @objc public var typeMsg = ""
   
}


typealias TF_didSelectRowAtIndexPathBlock = (_ expressionName: TFMember ) -> Void


class TF_LivingServicePageView: UIView ,UITableViewDelegate,UITableViewDataSource {

    /**所有私聊聊天数据*/
    @objc public var customerServiceDict = NSMutableDictionary.init()
    @objc public var didSelectBlock : TF_didSelectRowAtIndexPathBlock!
    
    @objc public var title = UILabel()
    @objc public var array = NSMutableArray()
    @objc public  var tableview:UITableView =  UITableView()
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.clear
        

        self.title.numberOfLines = 0;
        self.addSubview(self.title)
        self.title.textAlignment = .center;
        self.title.text = "客服"
        self.title.font = UIFont.PingFang(size: 19)
        self.title.textColor = UIColor(red: 20/255.0, green: 23/255.0, blue: 26/255.0, alpha: 1.0)

        
        self.tableview = UITableView.init()
        self.tableview.backgroundColor = UIColor.clear
        self.tableview.separatorStyle = .none
        self.tableview.delegate = self;
        self.tableview.dataSource = self;
        addSubview(self.tableview)
       
        
        self.tableview.register(TF_LivingServicePage1ViewCell.self, forCellReuseIdentifier: "TF_LivingServicePage1ViewCell")
 

    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        self.title.frame = CGRect(x: 0, y: 20, width: self.frame.size.width, height: 26)

        self.tableview.frame = CGRect(x: 0, y: 62, width: self.frame.size.width, height: self.frame.size.height - 62)
        
       
  
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //所有在线的客服人员数据
    @objc public func updateXidUI1(list:[TFMember])
    {
        self.array.removeAllObjects()
        self.array.addObjects(from: list)
        self.tableview.reloadData()
    }
    
    /**
     *  告诉tableView第section组有多少行
     */
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.array.count
    }
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "TF_LivingServicePage1ViewCell") as! TF_LivingServicePage1ViewCell

       
        
        cell.backgroundColor = UIColor.clear
        let model = self.array[indexPath.row] as! TFMember

        cell.viewWidth = CGFloat(self.frame.size.width)
        cell.viewHeight = 90
        
        //所有私聊聊天数据
        let obj = self.customerServiceDict[model.xid]
        if (obj is TF_CustomerService) {
            let  customerService = obj as! TF_CustomerService
            //未读信息数
            cell.unread = customerService.unread;
            //最后一张聊天内容
            cell.typeMsg = customerService.typeMsg;
            //最后收到聊天数据的时间
            cell.timestamp = customerService.timestamp;
        }

     
        cell.model = model;
      
        
        
        return cell
    }
    //点击
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
           
        //  取出选中的cell模型
        let model = self.array[indexPath.row] as! TFMember

            //所有私聊聊天数据
        let obj = self.customerServiceDict[model.xid] ;
        
          if (obj is TF_CustomerService) {
              let customerService = obj as! TF_CustomerService
                customerService.unread =  0;
                customerService.timestamp = "0"
                customerService.typeMsg = ""
            }
            
        if ((self.didSelectBlock) != nil) {
            
            self.tableview.reloadData()
            
            self.didSelectBlock(model)
            
            }
        
        
        }
    
    // 设置cell高度
       func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
           return 90
       }
    
}
